/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.util.List;
import javax.swing.JOptionPane;
import presentation.controller.ControlPanelUpdateTeacher;
import vo.IValueObject;
import vo.TeacherVO;

/**
 *
 * @author Joanna 8a
 */
public class PanelUpdateTeacher extends javax.swing.JPanel {

    /**
     * Creates new form PanelUpdateTeacher
     *
     * @param opc 1: Modificar Profesor; 2: Eliminar Profesor :D
     */
    public PanelUpdateTeacher(int opc, AuxiliarFrame parent) {
        initComponents();
        this.parent = parent;
        List<IValueObject> teachers = new ControlPanelUpdateTeacher().getTeachers();
        for (IValueObject teacher : teachers) {
            jComboBoxTeachers.addItem((TeacherVO) teacher);
        }
        if (opc == 1) {
            parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Card2r.png")).getImage());
            setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Modificación de Profesores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(204, 0, 204))); // NOI18N
            jButtonSave.setText("Modificar");
            jSpinnerTeacherReputation.setModel(new javax.swing.SpinnerNumberModel(0, 0, 5, 1));
        } else if (opc == 2) {
            parent.setIconImage(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Card4.png")).getImage());
            setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Eliminación de Profesores", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(204, 0, 204))); // NOI18N
            jButtonSave.setText("Eliminar");
            jSpinnerTeacherReputation.setEnabled(false);
            jTextFieldTeacherEmail.setEnabled(false);
        }
        if (jComboBoxTeachers.getSelectedItem() != null){
            jComboBoxTeachersActionPerformed(null);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jSpinnerTeacherReputation = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldTeacherEmail = new javax.swing.JTextField();
        jButtonSave = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jComboBoxTeachers = new javax.swing.JComboBox();

        jLabel1.setText("Nombre");

        jLabel3.setText("Reputación");

        jLabel4.setText("Correo Electrónico");

        jButtonSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Save.png"))); // NOI18N
        jButtonSave.setText("Guardar");
        jButtonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveActionPerformed(evt);
            }
        });

        jButtonCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Cancel.png"))); // NOI18N
        jButtonCancel.setText("Cancelar");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jComboBoxTeachers.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxTeachersActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(77, 77, 77)
                        .addComponent(jComboBoxTeachers, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldTeacherEmail)
                            .addComponent(jSpinnerTeacherReputation, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxTeachers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jSpinnerTeacherReputation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldTeacherEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSave)
                    .addComponent(jButtonCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveActionPerformed
        TeacherVO vo = (TeacherVO) jComboBoxTeachers.getSelectedItem();
        vo.setReputation((double) jSpinnerTeacherReputation.getValue());
        vo.setEmail(jTextFieldTeacherEmail.getText());

        switch (jButtonSave.getText()){
            case "Modificar" :
                if (new ControlPanelUpdateTeacher().update(vo)){
                    JOptionPane.showMessageDialog(this, "Profesor modificado correctamente!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "El profesor no se ha podido actualizar", "Error", JOptionPane.ERROR_MESSAGE);
                }
                break;
            case "Eliminar" :
                if (new ControlPanelUpdateTeacher().delete(vo.getId())){
                    JOptionPane.showMessageDialog(this, "Profesor eliminado correctamente!", "Aviso", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(this, "El profesor está asignado a uno o mas dias de clase y no se ha podido eliminar", "Error", JOptionPane.ERROR_MESSAGE);
                }
                break;
        }

        parent.dispose();
    }//GEN-LAST:event_jButtonSaveActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        parent.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jComboBoxTeachersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxTeachersActionPerformed
        jSpinnerTeacherReputation.setValue(((TeacherVO)jComboBoxTeachers.getSelectedItem()).getReputation());
        jTextFieldTeacherEmail.setText(((TeacherVO)jComboBoxTeachers.getSelectedItem()).getEmail());
    }//GEN-LAST:event_jComboBoxTeachersActionPerformed
    private AuxiliarFrame parent;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonSave;
    private javax.swing.JComboBox jComboBoxTeachers;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSpinner jSpinnerTeacherReputation;
    private javax.swing.JTextField jTextFieldTeacherEmail;
    // End of variables declaration//GEN-END:variables
}
